package com.gict.antifraudback.utils.magnet;

import org.reflections.Reflections;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MagnetFactory {

    private static final Map<String, IMagnet> SOURCE = new HashMap<>();

    private static boolean init = false;

    private static IMagnet createSource(String source) {
        if (null == source) return null;
        if (BTLMMagnet.source.equals(source)) return new BTLMMagnet();
        return null;
    }

    public static IMagnet getSourceService(String source) {
        IMagnet magnet = SOURCE.get(source);
        if (magnet == null){
            magnet = createSource(source);
            if (magnet == null) {
                throw new RuntimeException("不存在磁力服务");
            }else {
                SOURCE.put(source, magnet);
            }
        }
        return magnet;
    }

    private static void init() {
        if (init){
            return;
        }
        Reflections reflections = new Reflections("com.gict.antifraudback.utils.magnet"); // 替换为你的包名

        Set<Class<? extends IMagnet>> classes = reflections.getSubTypesOf(IMagnet.class);

        for (Class<? extends IMagnet> implClass : classes) {
            try {
                IMagnet magnet = implClass.newInstance();
                SOURCE.put(magnet.getSource(), magnet);
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        init = true;
    }

    public static Set<String> allMagnetSource(){
        init();
        return SOURCE.keySet();
    }
}